package com.zxy.car.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zxy.car.config.GuliESConfig;
import com.zxy.car.dao.RecordDao;
import com.zxy.car.entity.RecordEntity;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.common.utils.PageUtils;
import com.zxy.common.utils.Query;

import com.zxy.car.dao.PayplanDao;
import com.zxy.car.entity.PayplanEntity;
import com.zxy.car.service.PayplanService;


@Service("payplanService")
public class PayplanServiceImpl extends ServiceImpl<PayplanDao, PayplanEntity> implements PayplanService {
    @Autowired
    private RestHighLevelClient client;
    @Autowired
    private RecordDao recordDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        try{
            List<PayplanEntity> list = baseMapper.selectList(null);

            BulkRequest bulkRequest = new BulkRequest();
            for(PayplanEntity payplanEntity: list) {
                IndexRequest indexRequest = new IndexRequest("payplan");
                indexRequest.id(payplanEntity.getId().toString());
                String jsonString = JSONObject.toJSONString(payplanEntity);
                indexRequest.source(jsonString, XContentType.JSON);
                bulkRequest.add(indexRequest);
            }

            BulkResponse bulk = client.bulk(bulkRequest, GuliESConfig.COMMON_OPTIONS);
            //再查询
            SearchRequest searchRequest = new SearchRequest("payplan");
            //查询条件
            String key1 = (String) params.get("buymoney");
            String key2 = (String) params.get("buymoney");
            String key3 = (String) params.get("addway");
            String key4 = (String) params.get("addway");
            String page = (String) params.get("page");
            String limit = (String) params.get("limit");


            //构建条件
            SearchSourceBuilder builder = new SearchSourceBuilder();
            //分页
            builder.from((Integer.valueOf(page)-1)*Integer.valueOf(limit));
            builder.size(Integer.valueOf(limit));
            //模糊条件
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();


            builder.query(boolQueryBuilder);
            //条件封装
            SearchRequest source = searchRequest.source(builder);
            System.out.println("条件:"+source);
            //执行
            SearchResponse resp = client.search(searchRequest, GuliESConfig.COMMON_OPTIONS);
            System.out.println("结果:"+resp);
            Long total = resp.getHits().getTotalHits().value;
            SearchHit[] hits = resp.getHits().getHits();
            List<PayplanEntity> collect = Arrays.stream(hits).map(h -> {
                PayplanEntity payplanEntity = JSONObject.parseObject(h.getSourceAsString(),PayplanEntity.class);
                return payplanEntity;
            }).collect(Collectors.toList());
            return new PageUtils(collect,total.intValue(),Integer.valueOf(limit),Integer.valueOf(page));
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

    @Override
    public void addPlan(Integer id) {
        RecordEntity recordEntity = recordDao.selectById(id);
        PayplanEntity payplanEntity  = new PayplanEntity();
        payplanEntity.setBuymoney(recordEntity.getBuymoney());
        payplanEntity.setBuytime(recordEntity.getBuytime());
        payplanEntity.setPlan(recordEntity.getPlan());
        payplanEntity.setFirstmoney(recordEntity.getPaybackmoney());
        payplanEntity.setLittlepay(recordEntity.getPayadd());
        payplanEntity.setPayadd(recordEntity.getPayadd());
        payplanEntity.setSum(recordEntity.getBuymoney()+recordEntity.getPayadd());
        baseMapper.insert(payplanEntity);
    }

    @Override
    public void backmoney(Integer id) {
        PayplanEntity payplanEntity = baseMapper.selectById(id);
        payplanEntity.setFirstmoney(payplanEntity.getSum()/payplanEntity.getPlan());
        payplanEntity.setSum(payplanEntity.getSum()-payplanEntity.getFirstmoney());
        baseMapper.updateById(payplanEntity);
    }

    @Override
    public void backmoney5m(Integer id) {
        PayplanEntity payplanEntity = baseMapper.selectById(id);
        payplanEntity.setFirstmoney(payplanEntity.getSum()/payplanEntity.getPlan());
        payplanEntity.setSum(0.0);
        baseMapper.updateById(payplanEntity);
    }

}